<?php
	/**
	 * @package AmpleHarvest
	 */
	require_once 'TableHeader.php';
	require_once 'cTABLE_Drop_Down_Options.php';
	/**
	 * @brief This class Describes the IRT Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 28 September 2012
	 * 	 - <b>Author</b>: Jay Novak (jay.novak@iswcorp.com)
	 * 	  - Initial Version
	 *
	 * @author Jay Novak
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cTABLE_IRT_Comments extends cTable
	{
		const C_INT_MAX_RESULTS_RETURNED = 500;
		const C_INT_DEFAULT_RESULTS_RETURNED = 100;
		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = null;  
		public $m_int_COL_RequestID = null;
		public $m_int_COL_Comment = null;
		public $m_int_COL_CommentDate = null;
		public $m_int_COL_CommenterID = null;
		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "IRT_Comments";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of the requests.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");
			
			$obj_COL_RequestID = new cColVarChar("requestID");
			$obj_COL_RequestID->set_Length(36);
			$obj_COL_RequestID->set_Comment("This column holds the RequestID.");
			$obj_COL_RequestID->set_Label("Request ID");
			
			$obj_COL_Comment = new cColText("comment");
			$obj_COL_Comment->set_Comment("This column holds the comment.");
			$obj_COL_Comment->set_Label("Comment");
			
			$obj_COL_CommentDate = new cColDateTime("comment_date");
			$obj_COL_CommentDate->set_Comment("This column holds date of the comment.");
			$obj_COL_CommentDate->set_Label("Date of the comment");
			
			$obj_COL_CommenterID = new cColVarChar("commenter_id");
			$obj_COL_CommenterID->set_Length(36);
			$obj_COL_CommenterID->set_Comment("This column holds commenter user ID.");
			$obj_COL_CommenterID->set_Label("User ID of the commenter");
				

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//			$this->m_obj_Connection->activate_Logging();
			//			$this->m_obj_Connection->activate_LogDebug();
			//			$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);			
		    $this->m_int_COL_RequestID = $this->add_Column($obj_COL_RequestID);
			$this->m_int_COL_Comment = $this->add_Column($obj_COL_Comment);
			$this->m_int_COL_CommentDate = $this->add_Column($obj_COL_CommentDate);
			$this->m_int_COL_CommenterID = $this->add_Column($obj_COL_CommenterID);
		}

		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			//to-do: make sure the comment matches up with some request. 
			
			return $this->insert_Data2DB();
			
		}
		/**
		 * This function is used to update data that is already in the database
		 
		public function update_Data2DB()
		{
			$this->add_ColumnQuery($this->m_int_COL_ID, $this->get_id());
			if ($this->do_Search() > 0)
			{
				$obj_RS = $this->get_ResultSet();

				$this->set_date_requested($obj_RS[0][$this->get_Column_Name($this->m_int_COL_DateRequested)]);
			}

			$this->clear_Update();
			$this->add_ColumnQuery($this->m_int_COL_ID, $this->get_id());
			$this->set_date_updated(date_format(new DateTime(), "Y-m-d H:i:s"));

			return parent::update_Data2DB();
		}
		*/
		/**
		 * This function is used to insert Data into the database
		 */
		public function insert_Data2DB()
		{
			$this->set_id($this->create_GUID());
			$this->set_comment_date(date_format(new DateTime(), "Y-m-d H:i:s"));
			
			return parent::insert_Data2DB();
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the Harvest Request to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the Query to check (GUID)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}
		
		/**
		 * Check the validity of the data about to be saved
		 * @since 0.1.1
		 * return boolean
		 */
		public function validate_New()
		{
			//if (!$this->get_url())
			//{
			//	return false;
			//}
			//if (!$this->get_description())
			//{
			//	return false;
			//}
			return true;
		}
		
		/**
		 * overrides parent method to add additional info from join tables:
		 *   Status Label as StatusLabel
		 *   User Name as UserName
		 */
		public function get_ResultSet() {
			return parent::get_ResultSet();
		}
		
		private function setupUserNames($arr_TempRS) {
			$obj_Table_Users = new cTABLE_Users();
		
			foreach($arr_TempRS as &$result) {
				$name = $obj_Table_Users->get_UserNameFromID($result['user_id']);
				print('here'.$result['user_id'].$name);
				$result['user_id'] = $name;
			}
		
			return $arr_TempRS;
		}
	}
?>
